package cc.geguol.iodemo.objectstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


/**
 * 作者：13687
 * 日期：2017/6/17
 * 项目：IODemo
 * 描述：将一个对象序列化到本地
 */
public class ObjectSerilizableDemo {
	public static void main(String[] args) {
/*		try {
			writeObj();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		try {
			readObj();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	private static void readObj() throws IOException, ClassNotFoundException {
		ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("./objs/persion.obj"));
		Person person = (Person) objectInputStream.readObject();
		if (person != null) {
			System.out.println(person);
		}
		objectInputStream.close();
	}

	private static void writeObj() throws IOException {
		FileOutputStream fileOutputStream = new FileOutputStream("./objs/persion.obj");
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
		Person person =  new Person("geugo",25);
		objectOutputStream.writeObject(person);
		objectOutputStream.close();
	}

}
class Person implements Serializable{
	@Override
	public String toString() {
		return "Person{" +
				"name='" + name + '\'' +
				", age=" + age +
				'}';
	}

	private static final long serialVersionUID = 6977402643848374753L;
	public String name;
	public int age;
	public transient int other = 110;// 我不被序列化

	public Person(String name, int age) {
		this.name = name;
		this.age = age;
	}
}
